Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Events To Watch For

The flow chart in Figure 4.1 shows some of the events we want to process. In this applet, we are only looking for keystrokes and mouse clicks. We override the handleEvent method to allow us to program for these events. We use the target property of the Event object, which is passed into the event handler, to look for a specific object. Then we can look for more specific events. Listing 4.1 contains a snippet of code that shows how we deal with the user entering a name in the TextArea NameField.

Listing 4.1 Trapping for the Enter key event in a specific object.

if (evt.target == NameField)
      {char c=(char)evt.key;
      if (c  == '\n')
          { Name=NameField.getText();
            return true;
         }
         else { return false; }
   }

The object evt is the local instantiation of the Event parameter that is part of the handleEvent method, as we’ll see later in the complete source code listing. We use the target property to see which object the event occurred in, then we look at the key property to see if the Enter key was pressed. The Java escape sequence for Enter is \n. The rest of the code shown in the listing is fairly straightforward: We compare the pressed key to the “enter” escape sequence, and if we come up with a match, we set the Name string variable to the text in the NameField using the TextArea getText method. Because we have processed the event, and we want to let the rest of the event handler know that we’ve dealt with it, we return true. If this wasn’t the key we were looking for in this specific object (NameField), we would return false so that the other event handling code could attempt to process this event.

Finishing Up

One of Java’s great strengths lies in its ability to automatically allocate and de-allocate memory for objects created in the program, so the programmer doesn’t have to. We primarily use the destroy method to close the database connection that we open in the applet. The JDBC driver that we used to connect to the data source is alerted to the fact that the program is exiting, so it can gracefully close the connection and flush input and output buffers.

Getting A Handle On The JDBC Essentials: The Complete Applet Source Code

Okay, enough talk, let’s get busy! The complete source code is shown in Listings 4.2 though 4.9. The HTML file that we use to call our applet is shown in Listing 4.10. I bet you’re not too keen on entering all that code. But wait! There’s no need to type it all in, just pull out the CD-ROM and load the source into your favorite editor or IDE. Don’t forget, though, that you need to have the JDBC driver installed, and you may need your CLASSPATH set so that the applet can find the driver. If you’re planning on loading the driver as a class along with the applet, make sure you put the driver in the same place as the applet. See Chapter 3 if you have trouble getting the applet to run and you keep getting the “Can’t Find a Driver” or “Class not found” error.


Tip:  Source code on the CD-ROM.
There’s no need to type in the source code because the Interactive Query applet can be found on the CD-ROM, as is true for all source code in this book.

The Look Of The Applet

As I promised earlier, we’re going to cover the details of user interface design and layout. Listing 4.2 covers the initialization of the user interface, as well as the normal “preliminaries” associated with Java programs. To help you along, I’ve included some comments to elaborate on the fine points that will help you to understand what’s going on and what we are doing.

Listing 4.2 Setting up the objects.

import  java.net.URL;
import  java.awt.*;
import  java.applet.Applet;
// These are standard issue with applets, we need the net.URL
// class because the database identifier is a glorified URL.

import  java.sql.*;
// These are the packages needed to load the JDBC kernel, known as the
// DriverManager.
import  imaginary.sql.*;
// These are the actual driver classes! We are using the msql JDBC
// drivers to access our msql database.

public class IQ extends java.applet.Applet {
// This is the constructor for the base applet. Remember that the applet
// name must match the file name the applet is stored in--this applet
// should be saved in a file called "IQ.java".

 Button ConnectBtn = new Button("Connect to Database");
 TextField QueryField = new TextField(40);
 TextArea OutputField = new TextArea(10,75);
 TextField NameField = new TextField(40);
 TextField DBurl = new TextField(40);
 Connection con;
// Here we create the objects we plan to use in the applet.
// The Connection object is part of the JDBC API, and is the primary way
// of tying the JDBC's function to the applet.

  String url = "";
  String Name = "";

GridBagLayout: It’s Easier Than It Seems!

In Listing 4.2, we set up the objects we’ll be using in the user interface. We loaded the necessary classes and the specific driver we will use in the applet. In Listing 4.3, we go through the init phase of the applet, where we set up the user interface. We use GridBagLayout, a Java layout manager, to position the components in the applet window. GridBagLayout is flexible and offers us a quick way of producing an attractive interface.

Listing 4.3 Setting up the user interface.

public void init() {
      QueryField.setEditable(true);
      OutputField.setEditable(false);
     NameField.setEditable(true);
     DBurl.setEditable(true);
// We want to set the individual TextArea and TextField to be editable so
// the user can edit the OutputField, where we plan on showing the
// results of the query.

 GridBagLayout gridbag = new GridBagLayout();
 GridBagConstraints Con = new GridBagConstraints();
// create a new instance of GridBagLayout and the complementary
// GridBagConstraints.

 setLayout(gridbag);
// Set the layout of the applet to the gridbag that we created above.
 setFont(new Font("Helvetica", Font.PLAIN, 12));
 setBackground(Color.gray);
// Set the font and color of the applet.

 Con.weightx=1.0;
 Con.weighty=0.0;
 Con.anchor = GridBagConstraints.CENTER;
 Con.fill = GridBagConstraints.NONE;
 Con.gridwidth = GridBagConstraints.REMAINDER;


Previous Table of Contents Next